package org.example.java.rmi;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

/**
* User: ZhangJuntao
* Date: 14-2-11
* Time: 上午9:45
*/
public class RmiServer extends UnicastRemoteObject implements RmiServerService {
  public static final String MESSAGE = "Hello world";

  public RmiServer() throws RemoteException {
    super(0);    // required to avoid the 'rmic' step, see below
  }

  public String getMessage() {
    return MESSAGE;
  }

  public static void main(String args[]) throws Exception {
    System.out.println("RMI server started");

    try { //special exception handler for registry creation
      LocateRegistry.createRegistry(1099);
      System.out.println("java RMI registry created.");
    } catch (RemoteException e) {
      //do nothing, error means registry already exists
      System.out.println("java RMI registry already exists.");
    }

    //Instantiate RmiServer
    RmiServer obj = new RmiServer();

    // Bind this object instance to the name "RmiServer"
    Naming.rebind("//192.168.1.165/helloWorld", obj);
    System.out.println("PeerServer bound in registry");
  }
}
